import {Fighter} from "./models/fighter";
import {Point} from "./models/point";
import {drawfirebloodeffic, getrandomaction, getXY} from "./comm";
import {Bulleteffect} from "./models/bulleteffect";
import {Enemy} from "./models/enemy";
import cus1 from "./images/cus1.png"
import g1 from "./sound/g1.wav"
import shoot1 from "./sound/shoot1.wav"
import change from "./sound/change.wav"
import change2 from "./sound/change2.wav"
import bybit from "./sound/bybit.wav"
import {Soldier} from "./models/soldier";
import {Japanese} from "./models/japanese";
import {A_soldier} from "./models/a_soldier";

const pt:HTMLCanvasElement = document.getElementById("paintP") as HTMLCanvasElement;
const ptctx: CanvasRenderingContext2D | null = pt.getContext("2d");
const btnref:HTMLInputElement=document.getElementById("btnref") as HTMLInputElement;
const adio=new Audio();  //HTMLAudioElement = document.getElementById("sdplay1") as HTMLAudioElement;
let p_name=prompt("请输入姓名：","zhanghua");
let p_live=prompt("玩家生命值：","15000");
let p_amou=prompt("弹药：","32");
if(p_name==null)
    p_name="";
if(p_live==null)
    p_live="100";
if(p_amou==null)
    p_amou="32"

let fighter = new Fighter("action3",
    0, 360, 280, 135,
    3, 0, p_name, Number(p_live), 25000,
    5, false, Number(p_amou), 32);
let mousepoint = new Point();
adio.src =shoot1; //require("./sound/shoot1.wav");
let weaponimage = new Image();
let stageimage = new Image();
let playerbackimage = new Image();
playerbackimage.src=require('./images/sel_bg.png');
// let djimage = new Image();
// djimage.src = require("./images/dj.png"); //"images/dj.png";
//adio.loop=true;
//
let bteffects = new Array();

let enemys = new Array();

let japaneses = new Array();

let soldiers=new Array();

let asoldiers=new Array();

//
let bloodeffictindex:number=0;
let bloddeffictimg=new Image();
bloddeffictimg.src=require('./images/bloodeffic1.png');

let yyimg=new Image();
yyimg.src=require('./images/yy.png');

btnref.onclick=reload;

function reload() {
    location.reload();
}

//自动装弹
function autoreloadammou() {
    //自动装弹
    if (fighter.ammunitionbak > 0) {
        adio.src =change2; //require("./sound/change.wav");
        adio.play();
        fighter.reloadammou = true,
            // fighter.ammunition = ammous,
            fighter.actiontype = "换弹夹";
        fighter.actionindex = 0;
        fighter.actionstart_y = 515;
        fighter.action_width = 255;
        fighter.actioncount = 2;
        fighter.action_height = 265;
        fighter.ratewidth = 60;
        fighter.rateheight = 80;
        fighter.delay = 1000;
        if (fighter.ammunitionbak - (fighter.ammunitionlimit - fighter.ammunition) >= 0) {
            fighter.ammunitionbak = fighter.ammunitionbak - (fighter.ammunitionlimit - fighter.ammunition);
            fighter.ammunition = fighter.ammunitionlimit;
        } else {
            fighter.ammunition = fighter.ammunition + fighter.ammunitionbak;
            fighter.ammunitionbak = 0;
        }
    }
}

pt.oncontextmenu = function (e) {
    return false;
}

//鼠标操作按下
pt.onmousedown = function (e) {
    if (e.button.toString() === "0") {
        if (fighter.ammunition > 0) {
            //左键操作
            fighter.isfire = true;
            adio.src =shoot1; //require("./sound/shoot1.wav");
        }

    } else if (e.button.toString() === "2") {
        //右键操作
        autoreloadammou();
    }
}
//鼠标抬起
pt.onmouseup = function (e) {
    fighter.isfire = false;
    //adio.pause();
}

pt.onmousemove = function (e) {
    let pp = getXY(pt, e);
    if (pp.x < (pt.width - 50)) {
        mousepoint = pp;
    }
}

    pt.width = 800;
    pt.height = 500;
    let p1 = new Point();

    //敌人
    // for (let i = 0; i < 5; i++) {
    //     let eny = new Enemy();
    //     eny.en_x = Math.floor(Math.random() * (600 - 0 + 1)) + 0;
    //     eny.en_y = Math.floor(Math.random() * (280 - 100 + 1)) + 100;
    //     eny.speed = Math.floor(Math.random() * (20 - 5 + 1)) + 5;
    //     eny.enadio.src=bybit;
    //     enemys.push(eny);
    // }

    let japimg=new Image();
    japimg.src=require('./images/abjs.png');//japsoilder1.png
  //japimg.src=require('./images/japsoilder1.png');
    let japdeadimg=new Image();
    japdeadimg.src=require('./images/abjs_dead2.png');
    for(let i=0;i<30;i++)
    {
        let tempjap:Japanese=new Japanese(
            "安倍晋三",100,japimg,japdeadimg,yyimg,
            Math.floor(Math.random() * (600 - 0 + 1)) + 0,
            Math.floor(Math.random() * (280 - 100 + 1)) + 100,
            0,0,20,40
        );
        japaneses.push(tempjap);
    }

    for(let i=0;i<100;i++)
    {
        let asoilder=new A_soldier(i.toString(),20,100,   Math.floor(Math.random() * (600 - 0 + 1)) + 0,
            Math.floor(Math.random() * (280 - 100 + 1)) + 100);
        asoldiers.push(asoilder);
    }

    // //士兵
    // for(let i=0;i<20;i++) {
    //      let soilder=new Soldier();
    //      soilder.ini();
    //      soldiers.push(soilder);
    // }

    for (let i = 0; i < 400; i++) {
        var beffct = new Bulleteffect();
        beffct.img.src = require("./images/zdtx.png");
        bteffects.push(beffct);
    }

    weaponimage.src = require("./images/weapons.png");
    stageimage.src = require("./images/stage5.png");
    // fighter = new Fighter("action3",
    //     0, 360, 280, 135,
    //     3, 0, "hhh", 1000, 20000,
    //     5, false, 32, 32);

    let waringflag = false;

    var timer = setInterval(function () {
        pt.style.cursor = "url("+ cus1+") 20 20,auto"
        //pt.style.cursor ="crosshair";  //require("./images/cus1.png");
        if (ptctx != null) {
            if (!fighter.reloadammou && fighter.isfire && fighter.ammunition > 0) {
                fighter.ammunition = fighter.ammunition - 1;

                for (let i = 0; i < bteffects.length; i++) {
                    if (!bteffects[i].visable) {
                        // console.log(bteffects[i])
                        bteffects[i].visable = true;
                        bteffects[i].x = mousepoint.x;
                        bteffects[i].y = mousepoint.y;
                        break;
                    }
                }
                adio.play();
                if (fighter.actionindex < fighter.actioncount - 1)
                    fighter.actionindex = fighter.actionindex + 1;
                else
                    fighter.actionindex = 0;

                if (fighter.ammunition <= 0)
                    autoreloadammou(); //自动装弹
            } else {
                if (!fighter.reloadammou) {
                    fighter.actionindex = 0;
                } else {
                    if (fighter.actionindex < fighter.actioncount) {
                        var timerintelver = setTimeout(() => {
                            fighter.actionindex = fighter.actionindex + 1;
                        }, 200)
                    } else {
                        fighter.reloadammou = false;
                        fighter.actionindex = 0;
                        fighter.actiontype = "射击";
                        fighter.reloadammou = false;
                        fighter.actionstart_y = 360;
                        fighter.action_width = 280;
                        fighter.actioncount = 3;
                        fighter.action_height = 135;
                        fighter.ratewidth = 50;
                        fighter.rateheight = 50;
                        fighter.delay = 100;
                        adio.src = shoot1;
                    }
                }
            }
            ptctx.beginPath();
            ptctx.clearRect(0, 0, pt.width, pt.height);
            ptctx.rect(0, 0, pt.width - 1, pt.height - 1);
            ptctx.stroke();
            ptctx.rect(0, 400, pt.width - 1, 100);
            ptctx.stroke();
            //  ptctx.fillStyle = 'rgba(0, 255, 255, 0)';
            //action1  265,180    2    x:0 y:0
            //action2
            //action3  280,135    3    x:0 y:360
            // //绘制背景图
            // ptctx?.drawImage(stageimage, 0 ,
            //     0, 1100, 400);
            ptctx?.drawImage(playerbackimage, 0 ,0,playerbackimage.width,playerbackimage.height,
                0,400, pt.width, 100);
            ptctx?.drawImage(weaponimage, 0 + fighter.action_width * fighter.actionindex,
                fighter.actionstart_y, fighter.action_width, fighter.action_height, mousepoint.x, 400 - fighter.rateheight,
                fighter.ratewidth, fighter.rateheight);


            fighter.drawlive(ptctx);
            // ptctx?.drawImage(bteffects[0].img,0,0,127,127,10,10,20,20);
            for (let i = 0; i < bteffects.length; i++) {
                if (bteffects[i].visable) {
                    ptctx?.drawImage(bteffects[i].img, bteffects[i].img_x,
                        bteffects[i].img_y, bteffects[i].dwidth, bteffects[i].dheight,
                        bteffects[i].x - bteffects[i].rwidth,
                        bteffects[i].y - bteffects[i].rheight, bteffects[i].rwidth, bteffects[i].rheight);
                    // console.log(bteffects[i]);
                }
            }
            for (let i = 0; i < enemys.length; i++) {
                if (enemys[i].enlive > 0) {
                    if (fighter.isfire) {
                        if (mousepoint.x + 5 >= enemys[i].en_x && mousepoint.x + 5 <= enemys[i].en_x + enemys[i].enwidth) {
                            if (mousepoint.y + 5 > enemys[i].en_y && mousepoint.y + 5 <= enemys[i].en_y + enemys[i].enheight) {
                                enemys[i].enlive = enemys[i].enlive - fighter.demage;
                                if (enemys[i].enlive <= 0) {
                                    fighter.kills = fighter.kills + 1;
                                    enemys[i].enadio.play();

                                }
                            }
                        }
                    }
                }
            }

            for(let i=0;i<japaneses.length;i++)
            {
                if(japaneses[i].live>0)
                {
                    if (fighter.isfire && fighter.ammunition>0) {
                        if (mousepoint.x + 10 >= japaneses[i].x && mousepoint.x + 10 <= japaneses[i].x + japaneses[i].jpratewidth+10) {
                            if (mousepoint.y + 10 > japaneses[i].y && mousepoint.y + 10 <= japaneses[i].y + japaneses[i].jprateheight+20) {
                                japaneses[i].live = japaneses[i].live - fighter.demage;
                                //血溅效果
                                //ptctx.drawImage(bloddeffictimg,0,0,bloddeffictimg.width,bloddeffictimg.height,mousepoint.x,mousepoint.y,10,10);
                                if (japaneses[i].live <= 0) {
                                    fighter.kills = fighter.kills + 1;
                                }
                            }
                        }
                    }
                }
            }

            // //士兵受到攻击
            // for (let i = 0; i < soldiers.length; i++) {
            //     if (soldiers[i].live > 0) {
            //         if (fighter.isfire) {
            //             if (mousepoint.x + 5 >= soldiers[i].x && mousepoint.x + 5 <= soldiers[i].x + soldiers[i].ratewidth) {
            //                 if (mousepoint.y + 5 > soldiers[i].y && mousepoint.y + 5 <= soldiers[i].y + soldiers[i].rateheight) {
            //                     soldiers[i].live = soldiers[i].live - fighter.demage;
            //                     if (soldiers[i].live <= 0) {
            //                         fighter.kills = fighter.kills + 1;
            //                     }
            //                 }
            //             }
            //         }
            //     }
            // }

            //士兵受到攻击
            for (let i = 0; i < asoldiers.length; i++) {
                if (asoldiers[i].live > 0) {
                    if (fighter.isfire) {
                        if (mousepoint.x + 5 >= asoldiers[i].x && mousepoint.x + 5 <= asoldiers[i].x + asoldiers[i].ratewidth) {
                            if (mousepoint.y + 5 > asoldiers[i].y && mousepoint.y + 5 <= asoldiers[i].y + asoldiers[i].rateheight) {
                                asoldiers[i].live = asoldiers[i].live - fighter.demage;
                                if (asoldiers[i].live <= 0) {
                                    fighter.kills = fighter.kills + 1;
                                }
                            }
                        }
                    }
                }
            }

            //绘制死人
            for (let i = 0; i < enemys.length; i++) {
                enemys[i].enemydeaddraw(ptctx);
            }

            //绘制活人
            for (let i = 0; i < enemys.length; i++) {
                enemys[i].enemydraw(ptctx,pt.width,pt.height);
            }
            //
            // for(let i=0;i<japaneses.length;i++)
            // {
            //     if(japaneses[i].live>0) {
            //         japaneses[i].jpdrawshowde(ptctx);
            //     }
            // }

            // //绘制安倍晋三
            // for(let i=0;i<japaneses.length;i++)
            // {
            //     let tempobj: Japanese = japaneses[i];
            //     if(japaneses[i].live>0) {
            //         if(japaneses[i].live>50)
            //         {
            //             //japsoilder1.png  abjs.png  abjs2.png
            //             japimg.src= require('./images/abjs.png');
            //         }
            //         else
            //         {
            //             if(tempobj.actionindex==0) {
            //                 //abjs.png
            //                 japimg.src= require('./images/abjs.png');
            //                 tempobj.actionindex=1;
            //             }
            //             else {
            //                 //abjs2.png
            //                 japimg.src = require('./images/abjs2.png');
            //                 tempobj.actionindex=0;
            //             }
            //             tempobj.japimg =japimg;
            //         }
            //         tempobj.jpdraw(ptctx);
            //     }
            //     else
            //     {
            //         tempobj.jpdeaddraw(ptctx);
            //     }
            // }

            for(let i=0;i<asoldiers.length;i++) {
                asoldiers[i].setaction();
            }

            for(let i=0;i<asoldiers.length;i++) {
                if(asoldiers[i].live<=0)
                    asoldiers[i].deaddraw(ptctx);
            }


            for(let i=0;i<asoldiers.length;i++) {
                asoldiers[i].drawshowdo(ptctx);
            }

            for(let i=0;i<asoldiers.length;i++) {
                if(asoldiers[i].live>0) {
                    asoldiers[i].fire(fighter);
                    asoldiers[i].actiondraw(ptctx, pt.width, pt.height - 200);
                }

            }




           for(let i=0;i<soldiers.length;i++) {
               soldiers[i].setaction(fighter);
            }

            for(let i=0;i<soldiers.length;i++) {
                if( soldiers[i].live>0)
                {
                    soldiers[i].drawaction(ptctx);
                }
                else
                {
                    soldiers[i].drawdead(ptctx);
                }
            }

            //血溅效果
            for(let i=0;i<asoldiers.length;i++)
            {
                if(asoldiers[i].live>0)
                {
                    if (fighter.isfire) {
                        drawfirebloodeffic(ptctx,mousepoint.x ,mousepoint.y,asoldiers[i].x,asoldiers[i].y,asoldiers[i].ratewidth,asoldiers[i].rateheight);
                    }
                }
            }


            // //血溅效果
            // for(let i=0;i<japaneses.length;i++)
            // {
            //     if(japaneses[i].live>0)
            //     {
            //         if (fighter.isfire) {
            //             drawfirebloodeffic(ptctx,mousepoint.x ,mousepoint.y,japaneses[i].x,japaneses[i].y,japaneses[i].jpratewidth,japaneses[i].jprateheight);
            //         }
            //     }
            // }


            if (fighter.ammunitionbak <= 0) {
                ptctx.font = "oblique small-caps bold 35px Arial";
                if (waringflag) {
                    ptctx.fillStyle = "yellow";
                    waringflag = false;
                } else {
                    ptctx.fillStyle = "red";
                    waringflag = true;
                }
                ptctx.fillText("备用弹药已经耗尽！！！！", pt.width / 2 - 150, pt.height / 2);
                ptctx.fillText("补给等待" + fighter.ammundelayindex + "秒", pt.width / 2 - 80, pt.height / 2 + 38);
                ptctx.stroke();
            }
        }
    }, 100);
    var timer = setInterval(() => {

        if (fighter.ammunitionbak == 0) {
            if (fighter.ammundelayindex > 0)
                fighter.ammundelayindex = fighter.ammundelayindex - 1;
            else {
                fighter.ammunitionbak = 300;
                fighter.ammundelayindex = 20;
            }
        }

        for (let i = 0; i < bteffects.length; i++) {
            if (bteffects[i].visable) {

                if (bteffects[i].delayindex >= bteffects[i].delay) {
                    bteffects[i].delayindex = 0;
                    bteffects[i].visable = false;
                } else {
                    bteffects[i].delayindex = bteffects[i].delayindex + 1;
                }

            }
        }

    }, 1000)



